package gui.properties;

import gui.EditorWindowSettings;

import java.awt.Dimension;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JPanel;

import com.nightpiter.model.objects.Line;
import com.nightpiter.model.objects.Transfer;

import model.ModelChangeListener;
import model.objects.EdiLine;
import model.objects.EdiStation;
import model.objects.EdiTransfer;
import model.objects.EdiTunnel;


public class PropertiesPanel extends JPanel implements SelectionListener, ModelChangeListener{		
	
	private List<FlowPanel> panels = new LinkedList<FlowPanel>();
	
	public PropertiesPanel() {
		this.setPreferredSize(new Dimension(EditorWindowSettings.propertiesWidth,500));
		//this.setBorder(new javax.swing.border.TitledBorder("Properties"));
		this.setLayout(new ListLayout());
		
	}
	
	public void editLine(EdiLine line) {
		this.removeAll();
		panels.clear();
		LinePropertiesPanel linePanel = new LinePropertiesPanel(line); 
		panels.add(linePanel);
		this.add(linePanel);
		this.updateUI();
		
	}
	
	public void setStationsList(List<EdiStation> objects) {		
		if(objects==null) return;
		for(EdiStation c:objects) {
			FlowPanel panel = new StationPropertiesPanel(c);
			this.panels.add(panel);
			this.add(panel);
			panel.setPrVisible(true);
			for(Transfer transfer:c.getTransfers()) {
				FlowPanel panel2 = new TransferPropertiesPanel((EdiTransfer)transfer);
				this.panels.add(panel2);
				this.add(panel2);
				panel.setPrVisible(false);
			}
			
		}
		
	}

	@Override
	public void selectionSeted(List<EdiStation> stationSelection, List<EdiTunnel> tunnelSelection) {		
		this.removeAll();
		this.setStationsList(stationSelection);
		this.setTunnelsList(tunnelSelection);
		if(panels.size()>15) {
			for(FlowPanel p:panels)
				p.setPrVisible(false);
		}
		this.updateUI();
	}

	private void setTunnelsList(List<EdiTunnel> tunnelSelection) {
		if(tunnelSelection==null) return;
		for(EdiTunnel tunnel: tunnelSelection) {			
			
			FlowPanel panel = new TunnelPropertiesPanel(tunnel);
			this.panels.add(panel);
			this.add(panel);
			//panel.setPrVisible(true);
			//this.add(new StationPropertiesPanel(c));
		}		
	}

	@Override
	public void modelChanged(boolean newObjectsAdded) {
		for(FlowPanel p: panels) {
			p.updateData();
		}
		
	}

	
}
